import akka.actor.{Actor, ActorRef, ActorSystem, Props}

import scala.io.StdIn

class HelloActor extends Actor {
  //接收消息并进行处理
  override def receive: Receive = {
    case "吃了吗" => println("吃过了")
    case "吃的啥" => println("北京烤鸭")
    case "拜拜" => {
      //关闭自己
      context.stop(self)
      //关闭ActorSystem
      context.system.terminate()
    }
  }
}

object HelloActor {

  //通过ActorSystem创建线程池对象myFactory
  private val myFactory = ActorSystem("myFactory")
  //通过myFactory.actorOf来创建一个Actor
  private val helloActorRef: ActorRef = myFactory.actorOf(Props[HelloActor], "helloActor")

  def main(args: Array[String]): Unit = {

    println("--------------欢迎进入游戏世界----------------")
    println("**********************************************")
    println("****************猜拳开始**********************")
    println("**********************************************")

    var flag = true

    while (flag) {
      print("请输入发送的消息：")
      val consoleLine: String = StdIn.readLine()
      //!发送消息
      helloActorRef ! consoleLine
      if (consoleLine.equals("拜拜")) {
        flag = false
        println("程序即将结束！")
      }

      //让程序休眠100毫秒
      Thread.sleep(100)
    }
  }
}
